#   Trolls Outta Luckland v1.1.0
#   Copyright 2009-2010, Gummbum
#
#   This file is part of Trolls Outta Luckland.
#
#   Trolls Outta Luckland is free software: you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 3 of
#   the License, or (at your option) any later version.
#
#   Trolls Outta Luckland is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Trolls Outta Luckland.  If not, see
#   <http://www.gnu.org/licenses/>.

__version__ = '1.1.0'

### Python imports
import os
from pickle import Pickler, Unpickler
import sys

try:
    ### Game library imports
    import game_globals
    from game_globals import *
    import gumm_sound
except ImportError, e:
    print 'ImportError: %s' % e
    print '    in file "%s"' % os.path.abspath(sys.argv[0])

# Note: these do not hold the runtime values. They hold the values loaded
# at startup.
name = 'GUMMBUM'
master_volume = MASTER_VOLUME
fx_volume = FX_VOLUME
music_volume = MUSIC_VOLUME
fullscreen = True
visible_mouse = VISIBLE_MOUSE

def load():
    global name, master_volume, fx_volume, music_volume, fullscreen, \
        visible_mouse
    if os.access(SETTINGS_FILE, os.F_OK):
        if os.access(SETTINGS_FILE, os.R_OK):
            fh = open(SETTINGS_FILE, 'rb')
            unpickler = Unpickler(fh)
            game = game_globals.game
            (name,
             master_volume,
             fx_volume,
             music_volume,
             fullscreen,
             visible_mouse)= unpickler.load()
        else:
            raise pygame.error, 'settings file not readable: %s' % SETTINGS_FILE
        if not os.access(SETTINGS_FILE, os.W_OK):
            raise pygame.error, 'settings file not writeable: %s' % SETTINGS_FILE
    elif not os.access(os.getcwd(), os.W_OK):
        raise pygame.error, 'save directory not writeable: %s' % os.getcwd()

def save():
    fh = open(SETTINGS_FILE, 'wb')
    pickler = Pickler(fh)
    game = game_globals.game
    pickler.dump((
        game.name.text,
        gumm_sound.jukebox.get_master_volume(),
        game.fx_volume,
        game.music_volume,
        game.window.is_fullscreen(),
        game.pointer.visible,
    ))

load()
